{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{$title}</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" lay-filter="form">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">控制器编号</label>
            <div class="layui-input-4">
                <input type="text" name="control_id" lay-verify="required"  placeholder="请输入控制器编号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">控制器全称</label>
            <div class="layui-input-4">
                <input type="text" name="control_longname" lay-verify="required"  placeholder="请输入控制器全称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">控制器简称</label>
            <div class="layui-input-4">
                <input type="text" name="control_shortname" lay-verify="required"  placeholder="请输入控制器简称" class="layui-input">
            </div>
        </div>
	
        <div class="layui-form-item" >
            <label class="layui-form-label" style="width: 150px">大棚</label>
            <div class="layui-input-4">
                <select name="house_id" lay-verify="required"  lay-filter="hid">
                    <option value="">选择大棚</option>
                    {volist  name='house' id='vo'}
                    <option value="{$vo.id}">{$vo.house_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">选择网关</label>
            <div class="layui-input-4">
                <select name="gateway_id" lay-verify="required" id="gateway_id" lay-filter="gateway">
                </select>
            </div>
        </div>
		<div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">关闭状态图片</label>
            <input type="hidden" name="close_imgUrl" id="close_imgUrl">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="sensor_img1"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="sensor1">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
		<div class="layui-form-item">
            <label class="layui-form-label" style="width: 150px">开启状态图片</label>
            <input type="hidden" name="open_imgUrl" id="open_imgUrl">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="sensor_img2"><i class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="sensor2">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                <a onclick="navigate_back()" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>
    layui.use(['form', 'layer','upload','element'], function () {
        var form = layui.form, layer = layui.layer,$= layui.jquery,upload = layui.upload,element = layui.element;
        form.on('select(hid)', function (data) {
            $.ajax({
                url:'{:url("devControllerAdd")}',
                type:'post',
                data:{'house_id':data.value},
                success:function(res){
                    var opt = '';
                    if(res.length > 0){
                        for(var i = 0; i < res.length; i++){
                            opt +='<option value="'+res[i]['id']+'">'+res[i]['gateway_name']+'</option>';
                        }
                    }else {
                        opt ='<option >暂无网关</option>';
                    }
                    $("#gateway_id").html(opt);
                    form.render('select');
                }
            });
        });
		
		var uploadInst = upload.render({
            elem: '#sensor_img1'
            ,url: '{:url("UpFiles/upload")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#sensor1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res);
                //上传成功
                if(res.code>0){
                    $('#close_imgUrl').val(res.url);
                }else{
                    //如果上传失败
                    return layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
		var uploadInst2 = upload.render({
            elem: '#sensor_img2'
            ,url: '{:url("UpFiles/upload")}'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#sensor2').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                console.log(res);
                //上传成功
                if(res.code>0){
                    $('#open_imgUrl').val(res.url);
                }else{
                    //如果上传失败
                    return layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        form.on('submit(submit)', function (data) {
            loading =layer.load(1, {shade: [0.1,'#fff']});
            console.log(data);
            $.post('{:url("devControllerAdd")}', {control_id:data.field.control_id,gateway_id:data.field.gateway_id,control_longname:data.field.control_longname,control_shortname:data.field.control_shortname}, function (res) {
                layer.close(loading);
                if (res.code > 0) {
                    layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                        window.parent.location.reload();
                    });
                } else {
                    layer.msg(res.msg, {time: 1800, icon: 2});
                }
            });
        });
    });
    function navigate_back() {
        //parent.location.href="index";
        parent.layer.closeAll();
    }
</script>